package org.homeorganiser.flex.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.homeorganiser.flex.dto.Type;
import org.homeorganiser.flex.util.Constant;

public class TypeDAO implements IGenericDAO<Type> {

	private final DataSource dataSource;
	Connection conn = null;
	ResultSet rs = null;
	PreparedStatement ps;

	public TypeDAO(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	public List<Type> findAll(int type) {
		List<Type> typeList = new ArrayList<Type>();

		Type typeobj;
		try {

			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_SELECT_ALL_TYPE);
			ps.setInt(1, type);
			rs = ps.executeQuery();

			while (rs.next()) {
				typeobj = new Type();
				typeobj.setId(rs.getInt("id"));
				typeobj.setAddedBy(rs.getString("addedBy"));
				typeobj.setTypeName(rs.getString("typeName"));
				typeList.add(typeobj);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return typeList;

	}

	public List<Type> findByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public Type findById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	public Type create(Type typeobj) {
		try {
			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_INSERT_TYPE_TABLE,
					new String[] { "ID" });

			ps.setString(1, typeobj.getTypeName());
			ps.setInt(2, typeobj.getInventorytype());
			ps.setString(3, typeobj.getAddedBy());

			ps.executeUpdate();
			ResultSet rs = ps.getGeneratedKeys();
			rs.next();
			// Update the id in the returned object. This is important as this
			// value must be returned to the client.
			int id = rs.getInt(1);
			typeobj.setId(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
		return typeobj;
	}

	public boolean update(Type item) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean remove(Type item) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean removeAll(int type) {
		try {

			conn = this.dataSource.getConnection();
			ps = conn.prepareStatement(Constant.SQL_DELETE_ALL_TYPE);
			ps.setInt(1, type);
			return ps.executeUpdate() == 1;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
				if (ps != null) {
					ps.close();
				}
				if (conn != null) {
					conn.close();
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

	

}
